﻿; (function (window, undefined, $) {
    $(document).ready(function () {      
        var util = {};
        var options = {
            uploadUrl: "../Helper/UploadImg"
    };

    init();
    //初始化  
    function init() {
        //初始化图片上传  
        var btnImg = document.getElementById("btnUploadImg");
        var img = document.getElementById("imgShow");
        var TitlePic = document.getElementById("TitlePic");
        var Pictures = document.getElementById("Pictures");
        var fileName = document.getElementById("fileName");
        g_AjxUploadImg(btnImg, img, TitlePic, Pictures, fileName);
    }
    var g_AjxTempDir = "/FileUpload/";

    //图片上传  
    function g_AjxUploadImg(btn, img, TitlePic, Pictures, fileName) {
        var button = btn, interval;
        new AjaxUpload(button, {
            action: options.uploadUrl,
            data: {},
            name: 'myfile',
            onSubmit: function (file, ext) {
                if (!(ext && /^(jpg|JPG|png|PNG|gif|GIF)$/.test(ext))) {
                    alert("您上传的图片格式不对，请重新选择！");
                    return false;
                }
            },
            onComplete: function (file, response) {
                flagValue = response;
                if (flagValue == "-1") {
                    alert("您上传的图片格式不对，请重新选择！");
                }
                else if (flagValue == "-2") {
                    alert("您上传的图片大于200K，请重新选择！");
                }
                else if (flagValue == "-3") {
                    alert("图片上传失败！");
                }
                else {
                    alert("上传成功！");
                    var strs = new Array();
                    strs = response.split("/");

                    var Pictures2 = "";
                    var fileName2 = "";
                    for (var i = 0; i < strs.length; i++) {
                        if (i == 0) {
                            TitlePic.value = strs[i];
                        } else if (i == 1) {
                            Pictures.value = strs[i];
                            Pictures2 = strs[i];

                        } else if (i == 2) {
                            fileName2 = strs[i];
                        }
                    }
                    img.src = g_AjxTempDir + fileName2 + "/" + Pictures2;
                    fileName.value = g_AjxTempDir + fileName2 + "/" + Pictures2;
                }
            }
        });
    }
});
            
})(window, undefined, jQuery);